home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / SpriteWorld 2.2 / SpriteWorld Examples / Shark Attack / Sources & Headers / Application.c next >
Encoding:
C/C++ Source or Header  |  1999-01-25  |  12.5 KB  |  549 lines  |  [TEXT/CWIE]

  1. ///--------------------------------------------------------------------------------------
  2. // Application.c
  3. ///--------------------------------------------------------------------------------------
  4.  
  5. #include <Palettes.h>
  6. #include <SWIncludes.h>
  7. #include <SWGameUtils.h>
  8. #include <SWApplication.h>
  9.  
  10. #include "Application.h"
  11. #include "Shark Attack.h"
  12. #include "NewSprite.h"
  13. #include "Level.h"
  14. #include "SWSounds.h"
  15. #include "MyUtils.h"
  16. #include "Stats.h"
  17. #include "Special Effects.h"
  18. #include "GlobalVariables.h"
  19.  
  20.  
  21. #define        kMaxNumFishOnTitleScreen        4
  22.  
  23.  
  24. CTabHandle    gSavedEntriesH;
  25.  
  26.  
  27. ///--------------------------------------------------------------------------------------
  28. // Main
  29. ///--------------------------------------------------------------------------------------
  30.  
  31. void    main( void )
  32. {
  33.     OSErr err;
  34.     
  35.     Initialize(kNumberOfMoreMastersCalls);
  36.     Randomize();
  37.     
  38.     if (SWHasSystem7())
  39.     {
  40.         gTempRgn = NewRgn();    // Create temp region
  41.         AllowKeyUpEvents();
  42.         SetCursor(*GetCursor(watchCursor));
  43.         
  44.         if ( !IsNewSoundManagerInstalled() )
  45.         {
  46.             SetCursor(&qd.arrow);
  47.             StopAlert(130, NULL);
  48.             ExitToShell();
  49.         }
  50.         
  51.         SaveSystemVolume();
  52.         
  53.         err = CreateSoundChannels(4);
  54.         FatalError(err);
  55.         err = LoadSounds(128, kNumSounds);
  56.         FatalError(err);
  57.         
  58.         CreateWindow();
  59.         LoadPixPats();
  60.         SetUpSpriteWorld();
  61.         InitStats();
  62.         SetUpWindowRegions();
  63.         LoadSprites();
  64.         
  65.         CreateMenuBar();
  66.         SetupTitleScreen();
  67.         SetCursor(&qd.arrow);
  68.         
  69.         while (1)
  70.         {
  71.             ProcessEvents();
  72.             AnimateTitleScreen();
  73.         }
  74.     }
  75.     else
  76.     {
  77.         CantRunOnThisMachine();
  78.     }
  79. }
  80.  
  81.  
  82. ///--------------------------------------------------------------------------------------
  83. // ExitSharkAttack    - Clean up and quit
  84. ///--------------------------------------------------------------------------------------
  85.  
  86. void     ExitSharkAttack( void )
  87. {
  88.     // We don't even bother disposing this stuff, since the system will
  89.     // clean it up anyway. They are commented out so you can see what would
  90.     // need to be done if you did want to dispose everything yourself.
  91.  
  92. /*        
  93.     SWDisposeSpriteWorld(&gSpriteWorldP);
  94.     SWExitSpriteWorld();
  95.     SWDisposeWindowFrame(&gStatsWindowFrameP);
  96.     SWDisposeFrame(&gStatsBackFrameP);
  97.     SWDisposeFrame(&gStatsNumberFrameP);
  98.     DisposeSprites();    // This would dispose the master sprites not included in the SpriteWorld
  99.     DisposeSounds();
  100.     DisposeRgn(gTempRgn);
  101.     
  102.     DisposePixPat(gBackgroundPixPatH);
  103.     DisposePixPat(gTitleWaterPixPatH);
  104.     ReleaseResource((Handle)gGameWaterPictH);
  105. */
  106.  
  107.     DisposeSoundChannels();
  108.     SWShowMenuBar(gWindowP);
  109.     RestoreEventMask();
  110.     
  111.         // By hiding the window before quitting, we can avoid the brief discoloration
  112.         // of the window caused when switching back to the system palette. Also, by
  113.         // restoring the system palette ourselves, we can do it faster than ExitToShell.
  114.     HideWindow(gWindowP);
  115.     RestoreSystemPalette();
  116.     
  117.     RestoreSystemVolume();
  118.     
  119.     ExitToShell();
  120. }
  121.  
  122.  
  123. ///--------------------------------------------------------------------------------------
  124. // CreateMenuBar
  125. ///--------------------------------------------------------------------------------------
  126.  
  127. void    CreateMenuBar()
  128. {
  129.     Handle menuBarH;
  130.  
  131.     menuBarH = GetNewMBar(128);
  132.  
  133.     if (menuBarH != NULL)
  134.     {
  135.         SetMenuBar(menuBarH);
  136.         AppendResMenu(GetMenuHandle(mApple), 'DRVR');
  137.         DrawMenuBar();
  138.     }
  139.     else
  140.     {
  141.         CantFindResource();
  142.     }
  143.     
  144.     UpdateVolumeMenu();
  145. }
  146.  
  147.  
  148. ///--------------------------------------------------------------------------------------
  149. // CreateWindow
  150. ///--------------------------------------------------------------------------------------
  151.  
  152. void     CreateWindow( void )
  153. {
  154.     gWindowP = GetNewCWindow(128, NULL, (WindowPtr)-1L);
  155.  
  156.     if (gWindowP != NULL)
  157.     {
  158.         SizeWindow(gWindowP, qd.screenBits.bounds.right, 
  159.                         qd.screenBits.bounds.bottom, false);
  160.         MoveWindow(gWindowP, 0, 0, false);
  161.         ShowWindow(gWindowP);
  162.         
  163.         SetPort(gWindowP);
  164.         ForeColor(blackColor);
  165.         BackColor(whiteColor);
  166.     }
  167.     else
  168.         CantFindResource();
  169. }
  170.  
  171.  
  172. ///--------------------------------------------------------------------------------------
  173. // LoadPixPats
  174. ///--------------------------------------------------------------------------------------
  175.  
  176. void     LoadPixPats( void )
  177. {
  178.     gBackgroundPixPatH = GetPixPat(128);
  179.     if (gBackgroundPixPatH == NULL)
  180.         CantFindResource();
  181.     
  182.     gTitleWaterPixPatH = GetPixPat(129);
  183.     if (gTitleWaterPixPatH == NULL)
  184.         CantFindResource();
  185.     
  186.     gGameWaterPictH = GetPicture(129);
  187.     if (gGameWaterPictH == NULL)
  188.         CantFindResource();
  189. }
  190.  
  191.  
  192. ///--------------------------------------------------------------------------------------
  193. // SetUpWindowRegions - calculate the gOrigWindRgn and gPixPatWindRgn
  194. ///--------------------------------------------------------------------------------------
  195.  
  196. void     SetUpWindowRegions( void )
  197. {
  198.     Rect    windRect;
  199.     
  200.     SetPort(gWindowP);
  201.     
  202.     gOrigWindRgn = NewRgn();
  203.     gPixPatWindRgn = NewRgn();
  204.     GetClip(gOrigWindRgn);
  205.     GetClip(gPixPatWindRgn);
  206.     
  207.         // Subtract the SpriteWorld's windRect from the gPixPatWindRgn
  208.     windRect = gSpriteWorldP->windRect;
  209.     RectRgn(gPixPatWindRgn, &windRect);
  210.     DiffRgn(gOrigWindRgn, gPixPatWindRgn, gPixPatWindRgn);
  211. }
  212.  
  213.  
  214. ///--------------------------------------------------------------------------------------
  215. // ProcessEvents
  216. ///--------------------------------------------------------------------------------------
  217.  
  218. void    ProcessEvents( void )
  219. {
  220.     EventRecord        event;
  221.     
  222.     if ( WaitNextEvent(everyEvent, &event, kSleep, nil) )
  223.     {
  224.         switch ( event.what )
  225.         {
  226.             case mouseDown:
  227.                 HandleMouseDown( &event );
  228.                 break;
  229.             case keyDown:
  230.                 if ( (event.modifiers & cmdKey) != 0 )
  231.                 {
  232.                     HandleMenuChoice( MenuKey(event.message & charCodeMask) );
  233.                     break;
  234.                 }
  235.             case keyUp:
  236.                 ProcessKeyEvent(&event);    // This function is in Shark Attack.c
  237.                 break;
  238.             case updateEvt:
  239.                 UpdateWindow(&event);
  240.                 break; 
  241.             case osEvt:
  242.                 if ( ( event.message & suspendResumeMessage ) == resumeFlag )
  243.                 {
  244.                     ShowWindow(gWindowP);
  245.                 }
  246.                 else
  247.                 {
  248.                     HideWindow(gWindowP);
  249.                     RestoreSystemPalette();
  250.                 }
  251.                 break;
  252.         }
  253.     }
  254. }
  255.  
  256.  
  257. ///--------------------------------------------------------------------------------------
  258. // SetupTitleScreen
  259. ///--------------------------------------------------------------------------------------
  260.  
  261. void    SetupTitleScreen( void )
  262. {
  263.     gGameIsPaused = false;
  264.     
  265.         // Add the "Shark Attack" text to the SpriteWorld
  266.     AddTitleSprite();
  267.     
  268.         // Don't copy to the screen, because we'll get an updateEvent to do that
  269.     SWUpdateSpriteWorld(gSpriteWorldP, false);
  270.     ShowWindow(gWindowP);
  271. }
  272.  
  273.  
  274. ///--------------------------------------------------------------------------------------
  275. // AnimateTitleScreen
  276. ///--------------------------------------------------------------------------------------
  277.  
  278. void    AnimateTitleScreen( void )
  279. {
  280.     SWProcessSpriteWorld(gSpriteWorldP);
  281.     SWAnimateSpriteWorld(gSpriteWorldP);
  282.     
  283.         // Add fish periodically
  284.     if (gSpriteWorldP->frameHasOccurred)
  285.     {
  286.         if (gNumFishOnScreen < kMaxNumFishOnTitleScreen)
  287.         {
  288.             if (gFishDelay > 0)
  289.             {
  290.                 gFishDelay--;
  291.             }
  292.             else if (GetRandom(0, kFishRandomness) == 0)
  293.             {
  294.                 gFishDelay = kMinNewFishDelay;
  295.                 AddFish();
  296.             }
  297.         }
  298.     }
  299. }
  300.  
  301.  
  302. ///--------------------------------------------------------------------------------------
  303. // HandleMouseDown
  304. ///--------------------------------------------------------------------------------------
  305.  
  306. void    HandleMouseDown( EventRecord *eventPtr )
  307. {
  308.     WindowPtr        whichWindow;
  309.     short int        thePart;
  310.     long            menuChoice;
  311.     
  312.     thePart = FindWindow(eventPtr->where, &whichWindow);
  313.     
  314.     switch (thePart)
  315.     {
  316.         case inMenuBar:
  317.             menuChoice = MenuSelect(eventPtr->where);
  318.             HandleMenuChoice(menuChoice);
  319.             break;
  320.         case inSysWindow:
  321.             SystemClick(eventPtr, whichWindow);
  322.             break;
  323.     }
  324. }
  325.  
  326.  
  327. ///--------------------------------------------------------------------------------------
  328. // UpdateWindow
  329. ///--------------------------------------------------------------------------------------
  330.  
  331. void    UpdateWindow( EventRecord *eventPtr )
  332. {
  333.     if ( (WindowPtr)eventPtr->message == gWindowP )
  334.     {
  335.         BeginUpdate(gWindowP);
  336.         SetPort(gWindowP);
  337.         
  338.             // Draw the background pattern
  339.         SetClip(gPixPatWindRgn);
  340.         FillCRect(&gWindowP->portRect, gBackgroundPixPatH);
  341.         SetClip(gOrigWindRgn);
  342.         
  343.             // Update the SpriteWorld's worldRect
  344.         SWUpdateWindow(gSpriteWorldP);
  345.         
  346.             // Update stats area if a game is paused
  347.         if (gGameIsPaused)
  348.             UpdateStatsArea();
  349.         
  350.         EndUpdate(gWindowP);
  351.     }
  352. }
  353.  
  354.  
  355. ///--------------------------------------------------------------------------------------
  356. // HandleMenuChoice
  357. ///--------------------------------------------------------------------------------------
  358.  
  359. void    HandleMenuChoice( long menuChoice )
  360. {
  361.     short    menu;
  362.     short    item;
  363.     
  364.     if ( menuChoice != 0 )
  365.     {
  366.         menu = HiWord( menuChoice );
  367.         item = LoWord( menuChoice );
  368.         
  369.         switch ( menu )
  370.         {
  371.             case mApple:
  372.                 HandleAppleChoice( item );
  373.                 break;
  374.             case mFile:
  375.                 HandleFileChoice( item );
  376.                 break;
  377.             case mVolume:
  378.                 HandleVolumeChoice( item);
  379.                 break;
  380.         }
  381.         HiliteMenu( 0 );
  382.     }
  383. }
  384.  
  385.  
  386. ///--------------------------------------------------------------------------------------
  387. // HandleAppleChoice
  388. ///--------------------------------------------------------------------------------------
  389.  
  390. void    HandleAppleChoice( short item )
  391. {
  392.     MenuHandle    appleMenu;
  393.     Str255        accName;
  394.     short        accNumber;
  395.     OSErr        err;
  396.     
  397.     switch ( item )
  398.     {
  399.         case iAbout:
  400.             err = DoAboutBox();
  401.             FatalError(err);
  402.             break;
  403.         default:
  404.             appleMenu = GetMenuHandle(mApple);
  405.             GetMenuItemText(appleMenu, item, accName);
  406.             accNumber = OpenDeskAcc(accName);
  407.             break;
  408.     }
  409. }
  410.  
  411.  
  412. ///--------------------------------------------------------------------------------------
  413. // HandleFileChoice
  414. ///--------------------------------------------------------------------------------------
  415.  
  416. void    HandleFileChoice(short item)
  417. {
  418.     Boolean        stereoIsOn;
  419.     short        markChar;
  420.     
  421.     switch ( item )
  422.     {
  423.         case iNewGame:
  424.             NewGame();
  425.             break;
  426.         case iEndGame:
  427.             gGameOver = true;
  428.             gGameIsPaused = false;
  429.             break;
  430.         case iStereoSound:
  431.             GetItemMark(GetMenuHandle(mFile), iStereoSound, &markChar);
  432.             stereoIsOn = (markChar != checkMark);
  433.             CheckItem(GetMenuHandle(mFile), iStereoSound, stereoIsOn);
  434.             SetStereoMode(stereoIsOn);
  435.             break;
  436.         case iPauseGame:
  437.             gGameIsPaused = false;
  438.             break;
  439.         case iQuit:
  440.             ExitSharkAttack();
  441.             break;
  442.     }
  443. }
  444.  
  445.  
  446. ///--------------------------------------------------------------------------------------
  447. // HandleVolumeChoice
  448. ///--------------------------------------------------------------------------------------
  449.  
  450. void    HandleVolumeChoice(short menuItem)
  451. {
  452.     SetSystemVolume(menuItem-1);
  453.     PlaySound(kHitFishSnd, 2, kReplaceSameSound);
  454.     UpdateVolumeMenu();
  455. }
  456.  
  457.  
  458. ///--------------------------------------------------------------------------------------
  459. // UpdateVolumeMenu - place the check mark by the appropriate item
  460. ///--------------------------------------------------------------------------------------
  461.  
  462. void    UpdateVolumeMenu( void )
  463. {
  464.     short    volume, menuItem, n;
  465.     
  466.         // Get the current system volume
  467.     GetSystemVolume(&volume);
  468.     menuItem = volume+1;
  469.     
  470.         // Place the check by the corresponding item
  471.     for (n = 0; n <= 8; n++)
  472.         CheckItem(GetMenuHandle(mVolume), n, (menuItem == n) );
  473. }
  474.  
  475.  
  476. ///--------------------------------------------------------------------------------------
  477. // DoAboutBox
  478. ///--------------------------------------------------------------------------------------
  479.  
  480. OSErr    DoAboutBox(void)
  481. {
  482.     PicHandle     aboutPictH;
  483.     WindowPtr    aboutWindP;
  484.     OSErr        err = noErr;
  485.     
  486.     aboutPictH = GetPicture(kAboutPictResID);
  487.     if (aboutPictH == NULL)
  488.         err = ResError() ? ResError() : resNotFound;
  489.  
  490.     if (err == noErr)
  491.     {
  492.         aboutWindP = GetNewCWindow(kAboutWindResID, NULL, (WindowPtr)-1L);
  493.         if (aboutWindP == NULL)
  494.             err = ResError() ? ResError() : resNotFound;
  495.     }
  496.     
  497.     if (err == noErr)
  498.     {
  499.         PlaySound(kFishDeadSnd, 2, kFindEmptyChannel);
  500.             
  501.         SetWindowPic(aboutWindP, aboutPictH);
  502.         ShowWindow(aboutWindP);
  503.         
  504.         AboutBoxEventLoop();
  505.         
  506.             // First we clear the picture from the window
  507.         SetWindowPic(aboutWindP, NULL);
  508.             // Then we dispose it through the resource manager
  509.         ReleaseResource((Handle)aboutPictH);
  510.             // Finally we can dispose the window with no effect on the windowPic
  511.         DisposeWindow(aboutWindP);
  512.     }
  513.     
  514.     return err;
  515. }
  516.  
  517.  
  518. ///--------------------------------------------------------------------------------------
  519. // AboutBoxEventLoop
  520. ///--------------------------------------------------------------------------------------
  521.  
  522. void    AboutBoxEventLoop( void )
  523. {
  524.     EventRecord        event;
  525.     Boolean            done = false;
  526.     
  527.     do
  528.     {
  529.         if ( WaitNextEvent(everyEvent, &event, kSleep, nil) )
  530.         {
  531.             switch ( event.what )
  532.             {
  533.                 case mouseDown:
  534.                     done = true;
  535.                     break;
  536.                 case updateEvt:                // Update the window behind the About Box.
  537.                     UpdateWindow(&event);    // The About Box will update automatically,
  538.                     break;                     // since it has a windowPic installed.
  539.             }
  540.         }
  541.         else
  542.         {
  543.             if (!gGameIsPaused)        // Run animation if the game hasn't started yet
  544.                 AnimateTitleScreen();
  545.         }
  546.     } while (!done);
  547. }
  548.  
  549.